<?php
namespace app\shop\controller;

use x\Container;

class Goods extends Base
{


    public function lists()
    {
        if(is_post()){
            $d = $this->post;
            $map = [];
            $map[] = ["and","a.shop_id","=",$this->userinfo['id']];
            if($d['keyword']!=''){ $map[] = ['and','a.title','like',"%{$d['keyword']}%"]; }
            if($d['cat_id']!=''){ $map[] = ['and','a.cat_id','=',$d['cat_id']]; }
            if($d['rec_id']!=''){ $map[] = ['and','a.rec_id','=',$d['rec_id']]; }
            $w  = where($map);
            $rs = $this->db->query("select a.*,b.cat_title ptitle,c.title ctitle from goods a left join goods_cat b on a.cat_id = b.id left join goods_rec c on a.rec_id = c.id $w[0] order by a.sort desc,a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status'=>function($v){return c("type.goods_status.$v");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $rec = $this->db->query("select title,id from goods_rec order by sort desc,id desc");
        $cat = m('GoodsCat','c')->allSubCat();
        return $this->fetch(['rec'=>$rec,'cat'=>$cat]);
    }

    /**
     * 商品分类
     */
    public function goodsCat(){
        $d = $this->get();
        $id = "";
        if(isset($d['id'])){
            $id = $d['id'];
        }
        $map = [["and","parent_id","=","0"]];
        $w = where($map);
        $cat1 = $this->db->query("select * from goods_cat $w[0]",$w[1]);
        $this->fetch(["cat1" => $cat1,"id"=>$id]);
    }

    /**
     * 添加商品
     */
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
                'price' => "require",
                'pic' => "require",
                'postage' => "require",
                'content' => "require",
            ];
            $msg = [
                'title.require' => '商品名称不能为空',
                'price.require' => '商品价格不能为空',
                'pic.require' => '封面图不能为空',
                'postage.require' => '运费不能为空',
                'content.require' => '详情不能为空',
            ];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $d['shop_id'] = $this->userinfo['id'];
            $rs = m("goods", "c")->add($d);
            if ($rs) {
                m('Attachment','xcom')->imgSure('goods',$rs,$d);
                cookie('js_url',url('goods/lists'));
                cookie('js_msg','添加成功');
                return suc();
            }else{
                err(m("goods", "c")->getError());
            }
        }
        $cat_id = $this->get('cat_id');
        $cat_list  = m("goodsCat","c")->allSupCat($cat_id);
        $spec_list = m("goodsSpec","c")->catSpecList($cat_id);
        $d = [
            "spec_list"=>$spec_list,
            "cat_list"=>$cat_list
        ];
        $this->fetch($d);
    }

    /**
     * 修改商品
     */
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
                'price' => "require",
                'pic' => "require",
                'postage' => "require",
                'content' => "require",
            ];
            $msg = [
                'title.require' => '商品名称不能为空',
                'price.require' => '商品价格不能为空',
                'pic.require' => '封面图不能为空',
                'postage.require' => '运费不能为空',
                'content.require' => '详情不能为空',
            ];
            check($rule,$msg,$d);
            if(isset($d['pics'])){
                $d['pics'] = implode(",",$d["pics"]);
            }else{
                $d['pics'] = '';
            }
            $d['shop_id'] = $this->userinfo['id'];
            $rs = m("goods", "c")->edit($d);
            if ($rs) {
                m('Attachment','xcom')->imgSure('goods',$d['id'],$d);
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err(m("goods", "c")->getError());
            }
        }
        $d = $this->get();
        $goods_info = $this->db->find('goods',$d['id']);
        $goods_info['content'] = htmlspecialchars_decode($goods_info['content']);
        $goods_info['spec']    = htmlspecialchars_decode($goods_info['spec']);
        $goods_info['pics']    = explode(",",$goods_info["pics"]);

        //规格双引号处理
        $goods_cat_model = m("goodsCat","c");
        if(isset($d['cat_id'])){
            $goods_info['cat_id'] = $d['cat_id'];
        }
        //分类
        $cat_list = $goods_cat_model->allSupCat($goods_info['cat_id']);
        //规格
        $spec = json_decode($goods_info['spec'],true);
        foreach ($spec as $k=>$v){
            $spec[$k] = implode(",",$v);
        }
        //商品规格预览
        $goods_spec_list = $this->db->query("select * from goods_price where goods_id = {$goods_info['id']}");
        if($goods_spec_list){
            foreach ($goods_spec_list as $k=>$v){
                $goods_spec_list[$k]['spec'] = explode("_",$v['title']);
            }
        }
        $this->fetch(["goods_info"=>$goods_info,"cat_list"=>$cat_list,'spec'=>$spec,"goods_spec_list" => $goods_spec_list],'','layoutno');
    }

    public function detail()
    {

    }

    /**
     * 商品删除
     */
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('goods',$d['id']);
            m('Attachment','xcom')->imgDel('goods',$d['id'],true);
            $this->db->delete('goods_price',$d['id'],'goods_id');
            cookie('js_msg','删除成功');
            cookie('js_function','preload');
            return suc();
        }
    }


}

?>
